package cn.lysj.framework.coupon.service;

import cn.lysj.framework.base.BaseService;
import cn.lysj.framework.common.utils.pagination.PageUtil;
import cn.lysj.framework.common.utils.pagination.PageVo;
import cn.lysj.framework.common.utils.qrcode.QrCode;
import cn.lysj.framework.common.utils.random.CustomTimeType;
import cn.lysj.framework.common.utils.random.CustomTimeUtil;
import cn.lysj.framework.common.utils.random.FormatUtil;
import cn.lysj.framework.common.utils.web.ParamUtil;
import cn.lysj.framework.common.utils.web.Resp;
import cn.lysj.framework.coupon.domain.Coupon;
import cn.lysj.framework.coupon.repository.CouponRepository;
import cn.lysj.framework.coupon.vo.SelectCouponVo;
import cn.lysj.framework.customer.domain.CustomerVipCoupon;
import cn.lysj.framework.customer.service.CustomerVipCouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.method.P;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zwj
 * @description 优惠券服务层
 * @create 2019-01-16 09:38:37
 **/
@Slf4j
@Service
@Transactional
public class CouponService implements BaseService<Coupon> {
    @Value("${qr.code.url.prefix}")
    public String qrCodePrefix;

    @Resource
    private CouponRepository couponRepository;

    @Resource
    private CustomerVipCouponService customerVipCouponService;

    @Override
    public CouponRepository getRepository() {
        return couponRepository;
    }

    /**
     * @author ZhangWenJian
     * @date 2019/1/21 15:06
     * @title selectCouponVoToUseConfig
     * @param couponId
     * @param storeId
     * @return cn.lysj.framework.common.utils.web.Resp
     * @description 设置生日，会员 优惠券的下拉框
     */
    public Resp selectCouponVoToUseConfig(String[] couponId,String storeId){
        List<SelectCouponVo> selectCouponVos = new ArrayList<>();
        if(ParamUtil.isBlank(couponId)){
            selectCouponVos = couponRepository.findSelectCouponVoToUseConfig(storeId,1,2);
        }
        else{
            selectCouponVos = couponRepository.findSelectCouponVoToUseConfig(couponId,storeId,1,2);
        }
        for(int i = selectCouponVos.size()-1 ; i > 0 ;i--){
            if(selectCouponVos.get(i).getShareType() == 2 && selectCouponVos.get(i).getShare() == 0){
                selectCouponVos.remove(i);
            }
        }
        return new Resp(selectCouponVos);
    }

    /**
     * @author ZhangWenJian
     * @date 2019/1/18 16:15
     * @title customerVipGetCoupon
     * @param customerVipId
     * @param couponId
     * @param verify 是否需要需要验证
     * @return cn.lysj.framework.common.utils.web.Resp
     * @description 领取红包工具类
     */
    public Resp customerVipGetCoupon(String customerVipId,String couponId,Boolean verify){
        //是否需要判断用户已经领取过优惠券
        if(verify){
            if(customerVipCouponService.getRepository().findByCustomerVipIdAndCouponId(customerVipId,couponId).size() != 0){
                return new Resp(Resp.PARAM_ERROR,"领取失败,您已经领取过该优惠券了!");
            }
        }
        Coupon coupon = couponRepository.findByIdAndDelFlag(couponId,1);
        if(coupon == null){
            return new Resp(Resp.PARAM_ERROR,"优惠券已经失效啦!");
        }
        if(coupon.getDeadlineType() == 1){
            if(new Date().compareTo(coupon.getUseEndDate()) != -1 ){
                return new Resp(Resp.PARAM_ERROR,"优惠券已经过期啦!");
            }
        }
        if(coupon.getShareType() == 2){
            if(coupon.getShare() <= 0){
                return new Resp(Resp.PARAM_ERROR,"领取失败,优惠券已经被抢光啦!");
            }
            coupon.setShare(coupon.getShare()-1);
            couponRepository.save(coupon);
        }
        Date beginDate = new Date();
        Date endDate = new Date();
        if(coupon.getDeadlineType() == 1){
            beginDate = coupon.getUseBeginDate();
            endDate = coupon.getUseEndDate();
        }
        else if(coupon.getDeadlineType() == 2){
            beginDate = CustomTimeUtil.getSkewingTimeInDay(new Date(),0, CustomTimeType.BEGIN);
            endDate = CustomTimeUtil.getSkewingTimeInDay(new Date(),coupon.getValidPeriod()-1, CustomTimeType.END);
        }
        CustomerVipCoupon customerVipCoupon = new CustomerVipCoupon(customerVipId,couponId,1,beginDate,endDate);
        customerVipCoupon = customerVipCouponService.getRepository().saveAndFlush(customerVipCoupon);
        return new Resp(customerVipCoupon,"优惠券领取成功!");
    }

    /**
     * @author ZhangWenJian
     * @date 2019/1/16 10:20
     * @title storeBuildCoupon
     * @param storeId
     * @param entity
     * @return cn.lysj.framework.common.utils.web.Resp
     * @description 商户添加优惠券
     */
    public Resp storeBuildCoupon(String storeId , Coupon entity){
        if(ParamUtil.isBlank(entity.getName())){
            return new Resp(Resp.PARAM_ERROR,"请输入优惠券名称!");
        }
        if(entity.getFullReductionMoney() == null){
            return new Resp(Resp.PARAM_ERROR,"请输入满减金额!");
        }
        if(entity.getFullReductionMoney().compareTo(BigDecimal.ZERO) < 0){
            return new Resp(Resp.PARAM_ERROR,"满减金额不能小于0!");
        }
        if(entity.getDiscountMoney() == null){
            return new Resp(Resp.PARAM_ERROR,"请输入优惠金额!");
        }
        if(entity.getDiscountMoney().compareTo(BigDecimal.ZERO) <= 0){
            return new Resp(Resp.PARAM_ERROR,"优惠金额必须大于0!");
        }
        if(entity.getShareType() == 2){
            if(ParamUtil.isBlank(entity.getShare())){
                return new Resp(Resp.PARAM_ERROR,"请输入发放数量!");
            }
            if(entity.getShare() < 0){
                return new Resp(Resp.PARAM_ERROR,"发放数量必须大于0!");
            }
        }
        if(entity.getDeadlineType() == 1){
            if(ParamUtil.isBlank(entity.getUseBeginDate())){
                return new Resp(Resp.PARAM_ERROR,"请选择优惠券有效开始时间!");
            }
            entity.setUseBeginDate(CustomTimeUtil.getSkewingTimeInDay(entity.getUseBeginDate(),0,CustomTimeType.BEGIN));
            if(ParamUtil.isBlank(entity.getUseEndDate())){
                return new Resp(Resp.PARAM_ERROR,"请选择优惠券有效结束时间!");
            }
            entity.setUseEndDate(CustomTimeUtil.getSkewingTimeInDay(entity.getUseEndDate(),0,CustomTimeType.END));
        }
        if(entity.getDeadlineType() == 2){
            if(entity.getValidPeriod() == null){
                return new Resp(Resp.PARAM_ERROR,"请输入优惠券有效天数!");
            }
            if(entity.getValidPeriod() <= 0){
                return new Resp(Resp.PARAM_ERROR,"优惠券有效天数必须大于0!");
            }
        }
        entity.setStoreId(storeId);
        entity = couponRepository.saveAndFlush(entity);
        String url = qrCodePrefix +  "getcoupon?couponId=" + entity.getId() + "&storeId=" + entity.getStoreId(); //二维码内容
        entity.setQrCode(QrCode.createQrCode(url));
        return new Resp("创建优惠券成功!");
    }

}
